<?php

class money{
	public $linkk;
	public $all_id=array();
	public $arr_fx_money=array();
	public $all_money=0;
	
	//连接数据库参数
	function __construct($db_host,$db_user,$db_pwd,$db_name,$db_char){
		$this->linkk = mysqli_connect($db_host,$db_user,$db_pwd);
		mysqli_select_db($this->linkk,$db_name);
		mysqli_set_charset($this->linkk,$db_char);
	}
	//根据id查询他自己的消费总和
	function self_xf($id){
		$sql="select sum(money) from xiaofei where pid=$id;";
		$re= mysqli_query($this->linkk,$sql);
		$arr =array();
		while($row=mysqli_fetch_assoc($re)){
			$arr[]=$row;
		}
		mysqli_free_result($re);
//		mysqli_close($this->linkk);
		return $arr[0]['sum(money)']?$arr[0]['sum(money)']:0;
	}
	
	//根据id查询他所有下级的消费总和
	function all_xf($id,$i=0){
		if($i==0){
			$this->all_money=0;
		}
		$i++;
		$sql="select * from kehu where pid=$id";
		$re= mysqli_query($this->linkk,$sql);
		$arr =array();
		while($row=mysqli_fetch_assoc($re)){
			$arr[]=$row;
		}
		mysqli_free_result($re);
//		mysqli_close($this->linkk);
		foreach($arr as $v){
//			$this->all_id[]=$v['id'];
			$this->all_money += $this->self_xf($v['id']);
			$this->all_xf($v['id'],$i);
		}
		return $this->all_money;
	}
	
	//根据id查询他的下级id集合
	function arr_id($id,$i=0){
		$i++;
		$sql="select * from kehu where pid=$id";
		$re= mysqli_query($this->linkk,$sql);
		$arr =array();
		while($row=mysqli_fetch_assoc($re)){
			$arr[]=$row;
		}
		mysqli_free_result($re);
		foreach($arr as $v){
			$this->arr_id[$i][]=$v['id'];
			$this->arr_id($v['id'],$i);
			
		}
		return $this->arr_id;
	}
	
	//根据id集合的每一个id，查询总消费
	function arr_fx_money($arr_id){
		foreach($arr_id as $k=>$v){
			$num=count($v);
			for($i=0;$i<$num;$i++){
//				$this->arr_fx_money[$k][$v[$i]]=$this->self_xf($v[$i]);
				$this->arr_fx_money[$k] += $this->self_xf($v[$i]);
			}
		}
		return $this->arr_fx_money;
	}
	
	
}




?>